<?php
    //这是一个数据提交处理类
    class deal{
    	private $username;
    	private $userpasswd;
	private $sqllogin;
	private $sqlreg;
	private $result;
	private $connect;
	//这是一个构造方法  完成数据接收 判断操作类型
	function __construct($x,$y,$z) {
        include "config/config.php";
        global $db_hosts;
        global $db_name;
        global $db_pwd;
        global $db_user;
		$this->username=$x;
		$this->userpasswd=$y;
		$this->connect = new mysqli($db_hosts,$db_user,$db_pwd,$db_name);
		return $this->type($z);
    
	}
	
	//判断操作类型
	private function type($type){
		if ($type == '登录'){
			return $this->login($this->username);
		}elseif($type == '注册'){
			return $this->reg($this->username,$this->userpasswd);
		} else {
			return $this->_put($_POST['title'],$_POST['content'],$_POST['lable']);
		}
	}
	
	//接受登录请求
	private function login($username) {
		$this->sqllogin="select * from user where username='".$username."'";
		$this->result= $this->connect->query($this->sqllogin);
		if(!$this->result){
			echo "用户名不存在!!!!跟你说了,请输入正确的用户名或密码!没有帐号?前往<a class='text-main' href='reg.php'>注册</a>";
		}else{
			    list($id,$user,$passwd) = mysqli_fetch_array($this->result);
					if($passwd == $this->userpasswd && $user == $this->username){
						setcookie('name',$user,time()+(7*24*60*60));
						setcookie('passwd',$passwd,time()+(7*24*60*60));
						setcookie('id',$id,time()+(7*24*60*60));
				echo "登录成功,即将前往<a href='admin/index.php' class='text-main'>文章发布页面</a>";
			    } else {
				echo "密码错误!请<a href='login.php' class='text-main'>返回</a>重新填写";
			    }
		    }
    }
    
    //接受注册请求
    private function reg($user,$passwd){
	if($_COOKIE['yzm'] == $_POST['yzm']){
	$this->sqlreg = "INSERT INTO user (username,userpasswd,times) values ('".$user."','".$passwd."',NOW())";
	$this->connect->query($this->sqlreg);
	echo "注册成功,赶紧<a href='login.php' class='text-main'>登录</a>吧";
	} else {
	    echo "验证码输入错误,请<a href='reg.php' class='text-main'>返回</a>重新填写";
	}
	
    }
    
    //接受文章发表请求
    private function _put($title,$content,$lable){
	if ($title == "" || $content == ""){
		echo '可以发布没有标签,但不可以没有文章标题和内容&nbsp;&nbsp;||'."<a class='text-main' href='index.php'>返回重新发布</a>";
		}else{
		    $check = "/<script>/";
		    if (preg_match($check, $content) || preg_match($check, $title)) {
			echo "非法的内容,请不要使用&lt;script&gt;标签";
		} else {
			$sql = "INSERT INTO article(userid,title,content,lable,times) values ('$_COOKIE[id]','$title','$content','$lable',NOW())";
			$this->connect->query($sql);
			echo "发布成功啦!返回<a href='./' class='text-main'>首页</a>查看吧";
	    }
	}
    }
	}
